// 考试管理Mock数据
import { COURSES, CLASSES, generateStudents } from './mockData';
import dayjs from 'dayjs';

// 试卷接口
export interface ExamPaper {
  id: string;
  name: string;
  code: string;
  courseId: string;
  courseName: string;
  totalScore: number;
  duration: number; // 分钟
  questionCount: number;
  difficulty: "简单" | "中等" | "困难";
  status: "draft" | "published" | "archived";
  createTime: string;
  updateTime: string;
  creator: string;
  description?: string;
  tags: string[];
  isTemplate: boolean;
  usageCount: number;
  averageScore?: number;
  passRate?: number;
  questions?: ExamQuestion[];
}

// 题目接口
export interface ExamQuestion {
  id: string;
  type: "choice" | "programming" | "analysis" | "fill" | "essay";
  difficulty: "简单" | "中等" | "困难";
  score: number;
  content: string;
  answer: string;
  options?: string[];
  analysis?: string;
  knowledge: string[];
  tags?: string[];
}

// 考试接口
export interface Exam {
  id: string;
  name: string;
  paperId: string;
  paperName: string;
  courseId: string;
  courseName: string;
  startTime: string;
  endTime: string;
  duration: number; // 分钟
  status: "draft" | "published" | "ongoing" | "ended" | "graded";
  totalStudents: number;
  submittedCount: number;
  gradedCount: number;
  averageScore?: number;
  passRate?: number;
  createTime: string;
  publishTime?: string;
  selectedClasses: string[]; // 选中的班级ID
  selectedCourses: string[]; // 选中的课程ID
  settings: {
    allowLateSubmission: boolean;
    maxLateMinutes: number;
    allowReview: boolean;
    autoGrade: boolean;
    requireProctor: boolean;
  };
}

// 考试提交接口
export interface ExamSubmission {
  id: string;
  examId: string;
  examName: string;
  studentId: string;
  studentName: string;
  className: string;
  submitTime: string;
  status: "submitted" | "graded" | "published" | "late";
  score?: number;
  totalScore: number;
  timeSpent: number; // 分钟
  gradedBy?: string;
  gradedTime?: string;
  comments?: string;
  aiScore?: number;
  aiComments?: string;
  teacherScore?: number;
  teacherComments?: string;
  answers?: StudentAnswer[];
}

// 学生答案接口
export interface StudentAnswer {
  questionId: string;
  answer: string;
  score?: number;
  comments?: string;
}

// 生成试卷数据
export const generateMockPapers = (): ExamPaper[] => [
  {
    id: "p1",
    name: "数据结构与算法期中考试",
    code: "DS_MID_2024",
    courseId: "c1",
    courseName: "数据结构与算法",
    totalScore: 100,
    duration: 120,
    questionCount: 25,
    difficulty: "中等",
    status: "published",
    createTime: "2024-03-15 10:00:00",
    updateTime: "2024-03-15 10:00:00",
    creator: "张教授",
    description: "涵盖线性表、栈、队列、树、图等基本数据结构",
    tags: ["数据结构", "算法", "期中考试"],
    isTemplate: false,
    usageCount: 3,
    averageScore: 78.5,
    passRate: 85.2,
    questions: [
      {
        id: "q1",
        type: "choice",
        difficulty: "简单",
        score: 4,
        content: "以下哪种数据结构是先进先出的？",
        answer: "A",
        options: ["A. 队列", "B. 栈", "C. 树", "D. 图"],
        analysis: "队列的特点是先进先出(FIFO)，栈是先进后出(LIFO)",
        knowledge: ["队列", "栈"],
        tags: ["基础概念"]
      },
      {
        id: "q2",
        type: "programming",
        difficulty: "中等",
        score: 20,
        content: "实现一个栈的基本操作：push、pop、peek",
        answer: "class Stack { ... }",
        analysis: "需要实现栈的基本操作，注意边界条件处理",
        knowledge: ["栈", "编程实现"],
        tags: ["编程题"]
      }
    ]
  },
  {
    id: "p2",
    name: "Java程序设计期末考试",
    code: "JAVA_FINAL_2024",
    courseId: "c2",
    courseName: "Java程序设计",
    totalScore: 100,
    duration: 150,
    questionCount: 30,
    difficulty: "困难",
    status: "draft",
    createTime: "2024-03-10 14:30:00",
    updateTime: "2024-03-12 16:20:00",
    creator: "李副教授",
    description: "面向对象编程、异常处理、集合框架、多线程",
    tags: ["Java", "面向对象", "期末考试"],
    isTemplate: false,
    usageCount: 0,
    questions: [
      {
        id: "q3",
        type: "choice",
        difficulty: "中等",
        score: 3,
        content: "Java中哪个关键字用于继承？",
        answer: "B",
        options: ["A. implements", "B. extends", "C. super", "D. this"],
        analysis: "extends用于类继承，implements用于接口实现",
        knowledge: ["继承", "接口"],
        tags: ["面向对象"]
      }
    ]
  },
  {
    id: "p3",
    name: "数据库原理实验考试",
    code: "DB_LAB_2024",
    courseId: "c3",
    courseName: "数据库原理",
    totalScore: 50,
    duration: 90,
    questionCount: 15,
    difficulty: "简单",
    status: "published",
    createTime: "2024-03-08 09:15:00",
    updateTime: "2024-03-08 09:15:00",
    creator: "王讲师",
    description: "SQL语句编写、数据库设计、事务处理",
    tags: ["数据库", "SQL", "实验考试"],
    isTemplate: true,
    usageCount: 5,
    averageScore: 82.3,
    passRate: 92.1,
    questions: [
      {
        id: "q4",
        type: "analysis",
        difficulty: "简单",
        score: 10,
        content: "分析以下SQL语句的执行结果",
        answer: "查询结果分析...",
        analysis: "需要理解SQL语句的执行逻辑",
        knowledge: ["SQL", "查询分析"],
        tags: ["SQL分析"]
      }
    ]
  }
];

// 生成考试数据
export const generateMockExams = (): Exam[] => [
  {
    id: "e1",
    name: "数据结构与算法期中考试",
    paperId: "p1",
    paperName: "数据结构与算法期中考试",
    courseId: "c1",
    courseName: "数据结构与算法",
    startTime: "2024-04-15 14:00:00",
    endTime: "2024-04-15 16:00:00",
    duration: 120,
    status: "published",
    totalStudents: 98,
    submittedCount: 95,
    gradedCount: 92,
    averageScore: 78.5,
    passRate: 85.2,
    createTime: "2024-03-20 10:00:00",
    publishTime: "2024-03-25 09:00:00",
    selectedClasses: ["cls1", "cls2", "cls3"],
    selectedCourses: ["c1"],
    settings: {
      allowLateSubmission: true,
      maxLateMinutes: 30,
      allowReview: true,
      autoGrade: true,
      requireProctor: false,
    },
  },
  {
    id: "e2",
    name: "Java程序设计期末考试",
    paperId: "p2",
    paperName: "Java程序设计期末考试",
    courseId: "c2",
    courseName: "Java程序设计",
    startTime: "2024-05-20 09:00:00",
    endTime: "2024-05-20 11:30:00",
    duration: 150,
    status: "draft",
    totalStudents: 87,
    submittedCount: 0,
    gradedCount: 0,
    createTime: "2024-03-18 15:30:00",
    selectedClasses: ["cls1", "cls2"],
    selectedCourses: ["c2"],
    settings: {
      allowLateSubmission: false,
      maxLateMinutes: 0,
      allowReview: false,
      autoGrade: true,
      requireProctor: true,
    },
  },
  {
    id: "e3",
    name: "数据库原理实验考试",
    paperId: "p3",
    paperName: "数据库原理实验考试",
    courseId: "c3",
    courseName: "数据库原理",
    startTime: "2024-04-10 10:00:00",
    endTime: "2024-04-10 11:30:00",
    duration: 90,
    status: "ended",
    totalStudents: 72,
    submittedCount: 70,
    gradedCount: 68,
    averageScore: 82.3,
    passRate: 92.1,
    createTime: "2024-03-05 14:20:00",
    publishTime: "2024-03-08 16:00:00",
    selectedClasses: ["cls4", "cls5"],
    selectedCourses: ["c3"],
    settings: {
      allowLateSubmission: true,
      maxLateMinutes: 15,
      allowReview: true,
      autoGrade: false,
      requireProctor: false,
    },
  }
];

// 生成考试提交数据
export const generateMockSubmissions = (): ExamSubmission[] => {
  const allStudents = generateStudents(50, "计算机科学与技术", "2023级", 0);
  
  return [
    {
      id: "s1",
      examId: "e1",
      examName: "数据结构与算法期中考试",
      studentId: "20240001",
      studentName: "李明轩",
      className: "计科2301班",
      submitTime: "2024-04-15 15:45:00",
      status: "graded",
      score: 85,
      totalScore: 100,
      timeSpent: 110,
      gradedBy: "张教授",
      gradedTime: "2024-04-16 10:30:00",
      comments: "算法思路清晰，代码实现正确",
      aiScore: 83,
      aiComments: "客观题得分较高，主观题需要改进",
      teacherScore: 85,
      teacherComments: "整体表现良好，建议加强算法优化",
      answers: [
        {
          questionId: "q1",
          answer: "A",
          score: 4,
          comments: "正确"
        },
        {
          questionId: "q2",
          answer: "class Stack { ... }",
          score: 18,
          comments: "实现基本正确，但缺少边界检查"
        }
      ]
    },
    {
      id: "s2",
      examId: "e1",
      examName: "数据结构与算法期中考试",
      studentId: "20240002",
      studentName: "王雨桐",
      className: "计科2301班",
      submitTime: "2024-04-15 16:15:00",
      status: "graded",
      score: 92,
      totalScore: 100,
      timeSpent: 95,
      gradedBy: "张教授",
      gradedTime: "2024-04-16 11:15:00",
      comments: "优秀的表现，算法设计合理",
      aiScore: 90,
      aiComments: "所有题目都完成得很好",
      teacherScore: 92,
      teacherComments: "非常出色的答卷，继续保持",
      answers: [
        {
          questionId: "q1",
          answer: "A",
          score: 4,
          comments: "正确"
        },
        {
          questionId: "q2",
          answer: "class Stack { ... }",
          score: 20,
          comments: "实现完整，边界处理得当"
        }
      ]
    },
    {
      id: "s3",
      examId: "e3",
      examName: "数据库原理实验考试",
      studentId: "20240003",
      studentName: "张浩然",
      className: "数据2301班",
      submitTime: "2024-04-10 11:25:00",
      status: "late",
      score: 78,
      totalScore: 50,
      timeSpent: 85,
      gradedBy: "王讲师",
      gradedTime: "2024-04-11 09:00:00",
      comments: "逾期提交，但内容质量不错",
      aiScore: 76,
      aiComments: "SQL语句基本正确，但存在语法错误",
      teacherScore: 78,
      teacherComments: "内容正确，但逾期提交扣分",
      answers: [
        {
          questionId: "q4",
          answer: "查询结果分析...",
          score: 8,
          comments: "分析基本正确，但不够详细"
        }
      ]
    }
  ];
};

// 获取统计数据
export const getExamStatistics = () => {
  const papers = generateMockPapers();
  const exams = generateMockExams();
  const submissions = generateMockSubmissions();
  
  return {
    totalPapers: papers.length,
    publishedPapers: papers.filter(p => p.status === "published").length,
    totalExams: exams.length,
    ongoingExams: exams.filter(e => e.status === "ongoing").length,
    totalSubmissions: submissions.length,
    gradedSubmissions: submissions.filter(s => s.status === "graded").length,
    averageScore: submissions.filter(s => s.score).reduce((sum, s) => sum + (s.score || 0), 0) / submissions.filter(s => s.score).length,
  };
};

// 根据课程ID获取课程名称
export const getCourseNameById = (courseId: string): string => {
  const course = COURSES.find(c => c.id === courseId);
  return course ? course.name : "未知课程";
};

// 根据试卷ID获取试卷
export const getPaperById = (paperId: string): ExamPaper | undefined => {
  return generateMockPapers().find(p => p.id === paperId);
};

// 根据考试ID获取考试
export const getExamById = (examId: string): Exam | undefined => {
  return generateMockExams().find(e => e.id === examId);
};

// 根据考试ID获取提交记录
export const getSubmissionsByExamId = (examId: string): ExamSubmission[] => {
  return generateMockSubmissions().filter(s => s.examId === examId);
}; 
 
 